package com.xpc.controller.qin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.xpc.pojo.*;
import com.xpc.pojo.qin.*;

import com.xpc.service.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import static com.xpc.constant.PositionConstant.*;
import static com.xpc.constant.PositionStateConstant.*;

/**
 * @author Qinkuer
 * @version 1.0.0
 * @ProjectName UniversityGradeAdministrationSystem
 * @ClassName PositionManagementController.java
 * @Description TODO 班级信息
 * @createTime 2022年05月23日 15:45:00
 */
@RestController
@RequestMapping("positionManagement")
public class PositionManagementController {
    @Autowired
    private IMemberCommitteeClassService iMemberCommitteeClassService;

    @Autowired
    private StudentsSimpleInfoService studentsSimpleInfoService;

    @Autowired
    private RClassSimpleInfoService rClassSimpleInfoService;

    @Autowired
    private IUserService iUserService;

    @Autowired
    private ClassPositionSimpleInfoService classPositionSimpleInfoService;

    @Autowired
    private DepartmentSimpleInfoService departmentSimpleInfoService;

    @Autowired
    private DepartmentMembersInfoService departmentMembersInfoService;

    @Autowired
    private IMemberCommitteeGradeService iMemberCommitteeGradeService;

    /**
        * @description 获取所有班级的简单信息 已进行优化
     * 1. 查询class表,获取classID,className,majorID
     * 2. 通过majorID查询major表,获取专业的名称majorName
     * 3. 通过classid, 在student表中,获取各班级人数
     * 4. 通过classID获取 班委信息
     *
        * @param
        * @return  com.baomidou.mybatisplus.extension.api.R
        * @author Qinkuer
        * @Date  2022/5/23
        */
    @GetMapping("classCommitteeInfo")
    public R getClassSimpleInfo(){
//        ArrayList<RClassSimpleInfo> rClassSimpleInfos = new ArrayList<RClassSimpleInfo>();
//        List<MyClass> myClassList = iMyClassService.list();
//
//        for(MyClass mc:myClassList){
//            RClassSimpleInfo rClassSimple = new RClassSimpleInfo();
//            rClassSimple.setClassId(mc.getId());
//            rClassSimple.setMajorName(iMajorService.getById(mc.getMajorId()).getName());
//            rClassSimple.setStudentNums(iStudentService.count());
//            rClassSimple.setClassName(mc.getName());
//
//            //查询条件 通过设置查询条件 获取班级人数
//            QueryWrapper<Student> studentQueryWrapper1 = new QueryWrapper<Student>();
//            studentQueryWrapper1.eq("class_id",mc.getId());
//            rClassSimple.setStudentNums(iStudentService.count(studentQueryWrapper1));
//
//            //查询条件 通过设置条件 获取本班所有班委
//            QueryWrapper<MemberCommitteeClass> studentQueryWrapper2 = new QueryWrapper<MemberCommitteeClass>();
//            studentQueryWrapper2.eq("class_id",mc.getId());
//            List<MemberCommitteeClass> classCommitees = iMemberCommitteeClassService.list(studentQueryWrapper2);
//
//            //遍历班委, 获取班委的名字 ,职位
//            ArrayList<RClassSimpleInfo_ClassCommittee> rClassSimpleInfo_classCommittees = new ArrayList<>();
//            for(MemberCommitteeClass cm:classCommitees){
//                RClassSimpleInfo_ClassCommittee rClassSimpleInfo_classCommittee = new RClassSimpleInfo_ClassCommittee();
//                rClassSimpleInfo_classCommittee.setUserId(cm.getUserId());
//                //查询班委的名字
//                rClassSimpleInfo_classCommittee.setName(iUserService.getById(cm.getUserId()).getName());
//                //查询班委的职位名称
//                rClassSimpleInfo_classCommittee.setCommitteeName(iPositionService.getById(cm.getPositionId()).getName());
//
//                rClassSimpleInfo_classCommittees.add(rClassSimpleInfo_classCommittee);
//            }
//
//            rClassSimple.setClassCommitteesList(rClassSimpleInfo_classCommittees);
//            rClassSimpleInfos.add(rClassSimple);
//        }
//
//        return R.ok(rClassSimpleInfos);
        //怎么说呢 ,改进后查询效率提高了5倍...........
//        return R.ok(rClassSimpleInfoService.getAllClassSimpleInfo());

        //查询的结果是经过排序的, 所有能直接索引
        List<ClassPositionSimpleInfo> classList = classPositionSimpleInfoService.getList();
        List<ClassPositionSimpleInfo_Member> MLList = classPositionSimpleInfoService.getMLs();
        for(ClassPositionSimpleInfo_Member m:MLList){
            if(m.getPositionId()==1)
                classList.get(m.getClassId()-1).setMonitor(m);
            else
                classList.get(m.getClassId()-1).setLeagueBranchSecretary(m);
        }

        return R.ok(classList)   ;
    }


    /**
        * @description 根据班级编号,获取班干详细信息
        * @param classId 班级的编号
        * @return  com.baomidou.mybatisplus.extension.api.R
        * @author Qinkuer
        * @Date  2022/5/25
        */
    @GetMapping("classInfo/{classId}")
    public R getInformationByClassName(@PathVariable String classId){
        return R.ok(studentsSimpleInfoService.getListByClassID(classId));
    }



//    /**
//        * @description 修改增加某班级的班干
//        * @param cpm 存放操作人id,操作班级,以及操作信息.
//        * @return  com.baomidou.mybatisplus.extension.api.R
//        * @author Qinkuer
//        * @Date  2022/5/25
//        */
//    @PostMapping("classPositionChange")
//    public R classPositionChange(@RequestBody ClassPositionManagement cpm){
//        if(ObjectUtils.isEmpty(cpm.getClassId())||ObjectUtils.isEmpty(cpm.getUserId()))
//            return R.failed("classId或userId为空");
//        //用于更新user表
//        ArrayList<Long> add_longs = new ArrayList<>();
//        ArrayList<Long> update_longs = new ArrayList<>();
//
//        HashMap<Long, Integer> userId_committeeNumber = new HashMap<>();
//
//        List<MemberCommitteeClass> committeesChangeList = cpm.getCommitteesChangeList();
//        for(MemberCommitteeClass mcc:committeesChangeList){
//            //当改条数据是需要插入时. 判断班里的职位是否已经有人了.即每个职位只能有一个人
//            Timestamp timestamp = new Timestamp(new Date().getTime());
//            if(ObjectUtils.isEmpty(mcc.getId())){
//                QueryWrapper<MemberCommitteeClass> memberCommitteeClassQueryWrapper = new QueryWrapper<>();
//                memberCommitteeClassQueryWrapper.eq("position_id",mcc.getPositionId());
//                memberCommitteeClassQueryWrapper.eq("class_id",cpm.getClassId());
//                MemberCommitteeClass one = iMemberCommitteeClassService.getOne(memberCommitteeClassQueryWrapper);
//                //如果该职位已经被录入了
//                if(!ObjectUtils.isEmpty(one)){
//                    return R.failed("职位:"+mcc.getPositionId()+" 已被录入");
//                }
//                mcc.setCreator(cpm.getUserId());
//                mcc.setCreateDate(timestamp);
//
//                add_longs.add(mcc.getUserId());
//                //记录用户任职数量 绝大部分情况都是1
//                if(ObjectUtils.isEmpty(userId_committeeNumber.get(mcc.getUserId()))){
//                    userId_committeeNumber.put(mcc.getUserId(),1);
//                }else {
//                    userId_committeeNumber.put(mcc.getUserId(),userId_committeeNumber.get(mcc.getUserId())+1);
//                }
//
//            }
//            else {
//                update_longs.add(mcc.getUserId());
//            }
//            if(mcc.getPositionId()<CLASS_POSITION_START_NUMBER||mcc.getPositionId()>CLASS_POSITION_END_NUMBER)
//               return R.failed("输入的班委职位id: "+mcc.getPositionId()+"不存在");
//
//            mcc.setClassId(cpm.getClassId());
//            mcc.setModifier(cpm.getUserId());
//            mcc.setChangedDate(timestamp);
//            if(ObjectUtils.isEmpty(mcc.getPositionId())||ObjectUtils.isEmpty(mcc.getUserId()))
//                return R.failed("committeeChangeList存在空值,无法写入数据库");
//
//
//        }
//
////        for()
//
//
//        QueryWrapper<MemberCommitteeClass> allUpdateCommittee = new QueryWrapper<>();
//        QueryWrapper<MemberCommitteeClass> committeeUpdates = allUpdateCommittee.in("user_id", update_longs);
//        //职位更改卸任和升迁
//        iMemberCommitteeClassService.list()
//
//
//
//
//        if(iMemberCommitteeClassService.saveOrUpdateBatch(committeesChangeList)) {
//            //修改user表里面用户的职位.
//            QueryWrapper<User> userCommitteeAdd = new QueryWrapper<>();
//            QueryWrapper<User> userCommitteeUpdate = new QueryWrapper<>();
//            userCommitteeAdd.in("position_state",add_longs);
//            userCommitteeUpdate.in("position_state",update_longs);
//            List<User> userAddList = iUserService.list(userCommitteeAdd);
//            List<User> userUpdateList = iUserService.list(userCommitteeUpdate);
//            for(User i:userAddList){
//                int a=i.getPositionState();
//                i.setPositionState(a==STUDENT_POSITION_STATE_INT?COMMITTEE_CLASS_POSITION_STATE_INT:(a==COMMITTEE_GRADE_POSITION_STATE_INT?COMMITTEE_GRADE_CLASS_POSITION_STATE_INT:a));
//            }
//            for(User i:userUpdateList){
//                int a=i.getPositionState();
//                i.setPositionState(a==STUDENT_POSITION_STATE_INT?COMMITTEE_CLASS_POSITION_STATE_INT:(a==COMMITTEE_GRADE_POSITION_STATE_INT?COMMITTEE_GRADE_CLASS_POSITION_STATE_INT:a));
//            }
//
//
//            return R.ok(committeesChangeList);
//        }
//
//        return R.failed("插入,修改数据库操作失败,原因:可能与主外键约束有关");
//    }


    /**
        * @description 因为权限的原因,经过升级后的职位新增变更请求.逻辑更加完整
        * @param cpm 存放操作人id,操作班级,以及操作信息.
        * @return  com.baomidou.mybatisplus.extension.api.R
        * @author Qinkuer
        * @Date  2022/6/15
        */
    @PostMapping("classPositionChange")
    public R classPositionChange1(@RequestBody ClassPositionManagement cpm){
        if(ObjectUtils.isEmpty(cpm.getClassId())||ObjectUtils.isEmpty(cpm.getUserId()))
            return R.failed("classId或userId为空");
        List<MemberCommitteeClass> committeesChangeList = cpm.getCommitteesChangeList();

        QueryWrapper<MemberCommitteeClass> classId_MemberCommittees = new QueryWrapper<>();
        classId_MemberCommittees.eq("class_id",cpm.getClassId());
        List<MemberCommitteeClass> classId_MemberCommittees_list = iMemberCommitteeClassService.list(classId_MemberCommittees);
        //用户新任职的职位数量
        HashMap<Long, Integer> update_userId_committeeNumber = new HashMap<>();
        //原本任职的职位数量
        HashMap<Long, Integer> userId_committeeNumber = new HashMap<>();
        for(MemberCommitteeClass mcc: classId_MemberCommittees_list){
            mapSet(userId_committeeNumber,mcc.getUserId());
        }
        //减少任职的职位数量
        HashMap<Long, Integer> descrease_userId_committeeNumber = new HashMap<>();

        //必定是班委的用户的user_id
        ArrayList<Long> userId_NewCommittee = new ArrayList<>();

        //职位有变更的用户
        ArrayList<Long> userId_Change_arrayList = new ArrayList<>();

        for(MemberCommitteeClass mcc: committeesChangeList){
            Timestamp timestamp = new Timestamp(new Date().getTime());
            //判断职位或用户id为空不能插入
            if(ObjectUtils.isEmpty(mcc.getPositionId())||ObjectUtils.isEmpty(mcc.getUserId()))
                return R.failed("committeeChangeList存在空值,无法写入数据库");
            if(mcc.getPositionId()<CLASS_POSITION_START_NUMBER||mcc.getPositionId()>CLASS_POSITION_END_NUMBER)
                return R.failed("输入的班委职位id: "+mcc.getPositionId()+"不存在");

            mapSet(update_userId_committeeNumber,mcc.getUserId());

            userId_Change_arrayList.add(mcc.getUserId());

            //插入新职位
            if(ObjectUtils.isEmpty(mcc.getId())){
                mcc.setCreator(cpm.getUserId());
                mcc.setCreateDate(timestamp);
                mcc.setClassId(cpm.getClassId());
                mcc.setModifier(cpm.getUserId());
                mcc.setChangedDate(timestamp);
                userId_NewCommittee.add(mcc.getUserId());
                for(MemberCommitteeClass mcc2:classId_MemberCommittees_list){
                    if(cpm.getClassId().compareTo(mcc2.getClassId())==0&&mcc.getPositionId().compareTo(mcc2.getPositionId())==0){
                        //为要插入的数据,且职位已经存在,则不能录入
                            return R.failed("职位:"+mcc.getPositionId()+" 已被录入");
                    }
                }

            }else {
                //更新旧职位处理
                for(MemberCommitteeClass mcc2:classId_MemberCommittees_list){
                    if(cpm.getClassId().compareTo(mcc2.getClassId())==0&&mcc.getPositionId().compareTo(mcc2.getPositionId())==0){
                        mapSet(descrease_userId_committeeNumber,mcc2.getUserId());

                        userId_Change_arrayList.add(mcc2.getUserId());

                        mcc.setClassId(cpm.getClassId());
                        mcc.setModifier(cpm.getUserId());
                        mcc.setChangedDate(timestamp);
                    }
                }
            }

        }

        if(iMemberCommitteeClassService.saveOrUpdateBatch(committeesChangeList)){
            ArrayList<User> userArrayList = new ArrayList<>();
            //获取所有职位改变的用户
            QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
            userQueryWrapper.in("user_id",userId_Change_arrayList);
            List<User> userId_list = iUserService.list(userQueryWrapper);
            for(User i:userId_list){
                Integer a =i.getPositionState();
                Long i_user_id=i.getUserId();
                Integer i_position_state= i.getPositionState();
                if(ObjectUtils.isEmpty(update_userId_committeeNumber.get(i_user_id))){
                    //已知不是新任职的用户,则是卸任用户. 判读是否是完全卸任,在判断是否是年委班委合体
                    userArrayList.add(new User(i_user_id,
                            userId_committeeNumber.get(i_user_id)-descrease_userId_committeeNumber.get(i_user_id)>0?
                                    i_position_state:
                                    (i_position_state==COMMITTEE_GRADE_CLASS_POSITION_STATE_INT?COMMITTEE_GRADE_POSITION_STATE_INT:STUDENT_POSITION_STATE_INT)  ));
                }
                else {//新任职用户,必定升迁. 判断是否是年委,
                    userArrayList.add(new User(i_user_id,i_position_state==COMMITTEE_GRADE_POSITION_STATE_INT?COMMITTEE_GRADE_CLASS_POSITION_STATE_INT:COMMITTEE_CLASS_POSITION_STATE_INT));

                }
            }
            //更新用户状态 ,失败的话 也无法回滚上面的代码
            iUserService.saveOrUpdateBatch(userArrayList);
            return R.ok(committeesChangeList);

        }

        return R.failed("插入,修改数据库操作失败,原因:可能与主外键约束有关");
    }

    private void mapSet(HashMap<Long,Integer> map,Long key){
       if(ObjectUtils.isEmpty(map.get(key))){
           map.put(key,1);
       }
       else {
           map.put(key,map.get(key)+1);
       }
    }


    /**
        * @description TODO 删除对应的班级职位信息,或者是卸任某班干.进行准确的信息校对后,再删除
        * @param lmcc 必须包含职位信息表id, 班级id, 用户id, 职位id
        * @return  com.baomidou.mybatisplus.extension.api.R
        * @author Qinkuer
        * @Date  2022/6/7
        */
    @DeleteMapping("classPositionDelete")
    public R classPositionDelete(@RequestBody List_T<MemberCommitteeClass> lmcc){
        List<Integer> idlist=new ArrayList<Integer>();
        List<MemberCommitteeClass> mcc=lmcc.getData();
        for(MemberCommitteeClass i:mcc){
            if(ObjectUtils.isEmpty(i.getPositionId())||
                    ObjectUtils.isEmpty(i.getId())||
                    ObjectUtils.isEmpty(i.getClassId())||
                    ObjectUtils.isEmpty(i.getUserId())){
                return R.failed("输入信息不完整, 不能进行删除操作!");
            }
            idlist.add(i.getId());
        }
        List<MemberCommitteeClass> memberCommitteeClasses = iMemberCommitteeClassService.listByIds(idlist);
        int num=memberCommitteeClasses.size();
        for(MemberCommitteeClass j:memberCommitteeClasses){
            Integer positionId = j.getPositionId();
            for(MemberCommitteeClass k:mcc){
                if(j.getId().compareTo(k.getId())==0){
                    if(j.getClassId().compareTo(k.getClassId())!=0||
                            positionId.compareTo(k.getPositionId())!=0||
                            j.getUserId().compareTo(k.getUserId())!=0||
                            positionId>CLASS_POSITION_END_NUMBER||
                            positionId<CLASS_POSITION_END_NUMBER
                    ){
                        return R.failed("输入的信息错误, 不匹配数据库信息,原因可能是:不存在该任职信息,职位id不存在");
                    }
                    num--;
                }
            }
        }
        if(num>0){
            return R.failed("输入的信息有些在数据库中不存在");
        }
        if(iMemberCommitteeClassService.removeByIds(idlist)){
            return R.ok("删除了"+memberCommitteeClasses.size()+"条数据.");
        }

        return R.failed("数据库删除操作失败, 原因:可能是该数据已被删除");
    }


    /**
        * @description 获取年委各部门的粗略信息
        * @return  com.baomidou.mybatisplus.extension.api.R
        * @author Qinkuer
        * @Date  2022/5/26
        */
    @GetMapping("departmentInfo")
    public R departmentInformation(){
        List<DepartmentSimpleInfo> list = departmentSimpleInfoService.getList();
        List<DepartmentSimpleInfo_Member> topMembersList = departmentSimpleInfoService.getTopMembersList();
        for(DepartmentSimpleInfo_Member dm:topMembersList){
            //部长,党务部负责人,年级长
            if(dm.getPositionId()==MINISTER_INT
                    ||dm.getPositionId()==DEPARTMENT_PRINCIPAL_INT
                    ||dm.getPositionId()==GRADE_PRINCIPAL_INT){
                list.get(dm.getDepartmentId()-1).setMinister(dm);
            }
            //副部长,副年级长
            else if(dm.getPositionId()==UNDERSECRETARY_INT||dm.getPositionId()==DEPUTY_GRADE_PRINCIPAL_INT) {
                DepartmentSimpleInfo departmentSimpleInfo = list.get(dm.getDepartmentId() - 1);
                List<DepartmentSimpleInfo_Member> undersecretarys = departmentSimpleInfo.getUndersecretarys();
                if(undersecretarys==null){
                    departmentSimpleInfo.setUndersecretarys(new ArrayList<DepartmentSimpleInfo_Member>());
                    undersecretarys =departmentSimpleInfo.getUndersecretarys();
                    undersecretarys.add(dm);
                }
                else
                    undersecretarys.add(dm);
            }
        }

        return R.ok(list);
    }

    /**
        * @description 根据部门编号获取部门成员信息
        * @param departmentId 部门id.部门编号
        * @return  com.baomidou.mybatisplus.extension.api.R
        * @author Qinkuer
        * @Date  2022/5/26
        */
    @GetMapping("departmentMembersInfor/{departmentId}")
    public R departmentMembersInformation(@PathVariable Integer departmentId){
        return R.ok(departmentMembersInfoService.getMemberInforamtionByDepartmentId(departmentId));
    }

//    /**
//        * @description 修改或添加某个部门的成员
//        * @param dpm 携带信息
//        * @return  com.baomidou.mybatisplus.extension.api.R
//        * @author Qinkuer
//        * @Date  2022/5/26
//        */
//    @PostMapping("departmentPositionChange")
//    public R departmentPositionChange(@RequestBody DepartmentPositionManagement dpm){
//        if(ObjectUtils.isEmpty(dpm.getDepartmentId())||ObjectUtils.isEmpty(dpm.getUserId()))
//            return R.failed("departmentId或userId为空");
//
//        List<MemberCommitteeGrade> committeesChangeList = dpm.getCommitteesChangeList();
//        for(MemberCommitteeGrade mcg:committeesChangeList){
//            //当改条数据是需要插入时. 判断班里的职位是否已经有人了.即每个职位只能有一个人
//            Timestamp timestamp = new Timestamp(new Date().getTime());
//            if(ObjectUtils.isEmpty(mcg.getId())){
//                Integer positionId = mcg.getPositionId();
//
//                //部长,党务部负责人,主席团年级长 都只能有一位
//                if(positionId==MINISTER_INT||positionId==DEPARTMENT_PRINCIPAL_INT||positionId==GRADE_PRINCIPAL_INT){
//                    QueryWrapper<MemberCommitteeGrade> memberCommitteeGradeQueryWrapper = new QueryWrapper<>();
//                    memberCommitteeGradeQueryWrapper.eq("position_id",positionId);
//                    memberCommitteeGradeQueryWrapper.eq("d_c_g_id",mcg.getDCGId());
//                    MemberCommitteeGrade one = iMemberCommitteeGradeService.getOne(memberCommitteeGradeQueryWrapper);
//                    if(!ObjectUtils.isEmpty(one)){
//                        return R.failed("职位:"+mcg.getPositionId()+" 已被录入");
//                    }
//                }
//                mcg.setCreator(dpm.getUserId());
//                mcg.setCreateDate(timestamp);
//            }
//            if(mcg.getPositionId()>GRADE_POSITION_END_NUMBER||mcg.getPositionId()<GRADE_POSITION_START_NUMBER)
//                return R.failed("输入的年委职位id: "+mcg.getPositionId()+"不存在");
//            mcg.setDCGId(dpm.getDepartmentId());
//            mcg.setModifier(dpm.getUserId());
//            mcg.setChangedDate(timestamp);
//            if(ObjectUtils.isEmpty(mcg.getPositionId())||ObjectUtils.isEmpty(mcg.getUserId()))
//                return R.failed("committeeChangeList存在空值,无法写入数据库");
//        }
//        if(iMemberCommitteeGradeService.saveOrUpdateBatch(committeesChangeList))
//            return R.ok(committeesChangeList);
//        return R.failed("插入,修改数据库操作失败,原因:可能与主外键约束有关");
//    }

    /**
        * @description 新增或更新部门职位信息.
        * @param dpm 携带信息
        * @return  com.baomidou.mybatisplus.extension.api.R
        * @author Qinkuer
        * @Date  2022/6/15
        */
    @PostMapping("departmentPositionChange")
    public R departmentPositionChange1(@RequestBody DepartmentPositionManagement dpm){
        if(ObjectUtils.isEmpty(dpm.getDepartmentId())||ObjectUtils.isEmpty(dpm.getUserId()))
            return R.failed("classId或userId为空");
        List<MemberCommitteeGrade> committeesChangeList = dpm.getCommitteesChangeList();

        QueryWrapper<MemberCommitteeGrade> gradeId_MemberCommittees = new QueryWrapper<>();
        gradeId_MemberCommittees.eq("d_c_g_id",dpm.getDepartmentId());
        List<MemberCommitteeGrade> gradeId_MemberCommittees_list = iMemberCommitteeGradeService.list(gradeId_MemberCommittees);
        //用户新任职的职位数量
        HashMap<Long, Integer> update_userId_committeeNumber = new HashMap<>();
        //原本任职的职位数量
        HashMap<Long, Integer> userId_committeeNumber = new HashMap<>();
        for(MemberCommitteeGrade mcc: gradeId_MemberCommittees_list){
            mapSet(userId_committeeNumber,mcc.getUserId());
        }
        //减少任职的职位数量
        HashMap<Long, Integer> descrease_userId_committeeNumber = new HashMap<>();

        //必定是年委的用户的user_id
        ArrayList<Long> userId_NewCommittee = new ArrayList<>();

        //职位有变更的用户
        ArrayList<Long> userId_Change_arrayList = new ArrayList<>();

        for(MemberCommitteeGrade mcc: committeesChangeList){
            Timestamp timestamp = new Timestamp(new Date().getTime());
            //判断职位或用户id为空不能插入
            if(ObjectUtils.isEmpty(mcc.getPositionId())||ObjectUtils.isEmpty(mcc.getUserId()))
                return R.failed("committeeChangeList存在空值,无法写入数据库");
            if(mcc.getPositionId()<GRADE_POSITION_START_NUMBER||mcc.getPositionId()>GRADE_POSITION_END_NUMBER)
                return R.failed("输入的年委职位id: "+mcc.getPositionId()+"不存在");

            mapSet(update_userId_committeeNumber,mcc.getUserId());

            userId_Change_arrayList.add(mcc.getUserId());

            //插入新职位
            if(ObjectUtils.isEmpty(mcc.getId())){
                mcc.setCreator(dpm.getUserId());
                mcc.setCreateDate(timestamp);
                mcc.setDCGId(dpm.getDepartmentId());
                mcc.setModifier(dpm.getUserId());
                mcc.setChangedDate(timestamp);
                userId_NewCommittee.add(mcc.getUserId());
                for(MemberCommitteeGrade mcc2:gradeId_MemberCommittees_list){
                    Integer positionId = mcc2.getPositionId();
                    //部长,党务部负责人,主席团年级长 都只能有一位
                    if((mcc.getPositionId().compareTo(mcc2.getPositionId())==0)&&(positionId==MINISTER_INT||positionId==DEPARTMENT_PRINCIPAL_INT||positionId==GRADE_PRINCIPAL_INT)){
                        //为要插入的数据,且职位已经存在,则不能录入
                        return R.failed("职位:"+positionId+" 已被录入");
                    }
                    if(mcc.getUserId().compareTo(mcc2.getUserId())==0&&mcc.getPositionId().compareTo(mcc2.getPositionId())==0){
                        return R.failed("用户id:"+mcc2.getUserId()+"已经录入过该部门该职位");
                    }

                }


            }else {
                //更新旧职位处理
                for(MemberCommitteeGrade mcc2:gradeId_MemberCommittees_list){
                    if(dpm.getDepartmentId().compareTo(mcc2.getDCGId())==0&&mcc.getPositionId().compareTo(mcc2.getPositionId())==0){
                        mapSet(descrease_userId_committeeNumber,mcc2.getUserId());

                        userId_Change_arrayList.add(mcc2.getUserId());

                        mcc.setDCGId(dpm.getDepartmentId());
                        mcc.setModifier(dpm.getUserId());
                        mcc.setChangedDate(timestamp);
                    }
                }
            }

        }

        if(iMemberCommitteeGradeService.saveOrUpdateBatch(committeesChangeList)){
            ArrayList<User> userArrayList = new ArrayList<>();
            //获取所有职位改变的用户
            QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
            userQueryWrapper.in("user_id",userId_Change_arrayList);
            List<User> userId_list = iUserService.list(userQueryWrapper);
            for(User i:userId_list){
                Integer a =i.getPositionState();
                Long i_user_id=i.getUserId();
                Integer i_position_state= i.getPositionState();
                if(ObjectUtils.isEmpty(update_userId_committeeNumber.get(i_user_id))){
                    //已知不是新任职的用户,则是卸任用户. 判读是否是完全卸任,在判断是否是年委班委合体
                    userArrayList.add(new User(i_user_id,
                            userId_committeeNumber.get(i_user_id)-descrease_userId_committeeNumber.get(i_user_id)>0?
                                    i_position_state:
                                    (i_position_state==COMMITTEE_GRADE_CLASS_POSITION_STATE_INT?COMMITTEE_CLASS_POSITION_STATE_INT:STUDENT_POSITION_STATE_INT)  ));
                }
                else {//新任职用户,必定升迁. 判断是否是年委,
                    userArrayList.add(new User(i_user_id,i_position_state==COMMITTEE_CLASS_POSITION_STATE_INT?COMMITTEE_GRADE_CLASS_POSITION_STATE_INT:COMMITTEE_GRADE_POSITION_STATE_INT));

                }
            }
            //更新用户状态 ,失败的话 也无法回滚上面的代码
            iUserService.saveOrUpdateBatch(userArrayList);
            return R.ok(committeesChangeList);

        }

        return R.failed("插入,修改数据库操作失败,原因:可能与主外键约束有关");
    }

    /**
        * @description TODO 删除部门任职信息,即卸任某部门部员或者部门干部.
        * @param lmcg 包含任部门职信息id, 部门id, 用户id, 职位id
        * @return  com.baomidou.mybatisplus.extension.api.R
        * @author Qinkuer
        * @Date  2022/6/7
        */
    @DeleteMapping("departmentPositionDelete")
    public R departmentPositionDelete(@RequestBody List_T<MemberCommitteeGrade> lmcg){
        List<Integer> idlist=new ArrayList<Integer>();
        List<MemberCommitteeGrade> mcg = lmcg.getData();
        for(MemberCommitteeGrade i:mcg){
            if(ObjectUtils.isEmpty(i.getPositionId())||
                    ObjectUtils.isEmpty(i.getId())||
                    ObjectUtils.isEmpty(i.getDCGId())||
                    ObjectUtils.isEmpty(i.getUserId())){
                return R.failed("输入信息不完整, 不能进行删除操作!");
            }
            idlist.add(i.getId());
        }
        List<MemberCommitteeGrade> memberCommitteeGrades = iMemberCommitteeGradeService.listByIds(idlist);
        int num=memberCommitteeGrades.size();
        for(MemberCommitteeGrade j:memberCommitteeGrades){
            Integer positionId = j.getPositionId();
            for(MemberCommitteeGrade k:mcg){
                if(j.getId().compareTo(k.getId())==0){
                    if(j.getDCGId().compareTo(k.getDCGId())!=0||
                            positionId.compareTo(k.getPositionId())!=0||
                            j.getUserId().compareTo(k.getUserId())!=0||
                            positionId<GRADE_POSITION_START_NUMBER||
                            positionId>GRADE_POSITION_END_NUMBER
                    ){
                        return R.failed("输入的信息错误, 不匹配数据库信息,原因可能是:不存在该任职信息,职位id不存在");
                    }
                    num--;
                }
            }
        }
        if(num>0){
            return R.failed("输入的信息有些在数据库中不存在");
        }
        if(iMemberCommitteeGradeService.removeByIds(idlist)){
            return R.ok("删除了"+memberCommitteeGrades.size()+"条数据.");
        }

        return R.failed("数据库删除操作失败, 原因:可能是该数据已被删除");
    }

//    @Autowired
//    private HashMap<Integer,String> integerStringHashMap;
//
//    @Autowired
//    private IPositionService iPositionService;
//    @GetMapping("test")
//    public R test(){
//        List<Position> list = iPositionService.list();
//        integerStringHashMap.clear();
//        for (Position p:list){
//            integerStringHashMap.put(p.getId(),p.getName());
//        }
//        return R.ok(integerStringHashMap);
//    }
//
//    @GetMapping("test2")
//    public R test2(){
//        return R.ok(integerStringHashMap);
//    }




}

@Data
@AllArgsConstructor
@NoArgsConstructor
class List_T<T>{
    private List<T> data;
}